#include "test23_StlConOfList.h"
#include <iostream>



void test23_StlConOfList::classMain()
{
	std::cout << "test23_StlConOfList" << "\n";
	//this->testList();
	this->listToArray();
}


// list - List容器是一个双向链表
void test23_StlConOfList::testList()
{
	std::cout << "list测试\n";
	// list常用操作
	std::list<int> myList;
	for (int i = 0; i < 10; i++)
	{
		myList.push_back(i); // 把元素插入list的尾部
	}
	myList.push_front(88); // 头部插入

	std::cout << "list容器中的头部元素：" << myList.front() << std::endl;                       
	std::cout << "返回向量中第一个元素的迭代器：" << *myList.begin() << std::endl;                     
	std::cout << "list容器的最后一个元素：" << myList.back() << std::endl;                       
	std::cout << "返回容器的反向迭代器：(最后一个元素)" << *(myList.rbegin()) << std::endl;                   
	myList.pop_front(); // 删除一个头元素
	myList.pop_back();  // 删除一个尾部元素
	std::cout << "删除一个头元素后，第一个元素为：" << myList.front() << std::endl; 

	std::cout << "容器是否为空：" << myList.empty() << std::endl;  // 0
	std::cout << "容器大小/长度为" << myList.size() << std::endl; 

	myList.emplace_front(300); // 替换一个头元素
	myList.emplace_back(200); // 替换一个末尾元素
	
	

	// 三种遍历方式
	std::cout << "\n=== 基于迭代器的输出：\n";
	std::list<int>::iterator ele; // 定义一个迭代器/指针
	for (ele = myList.begin(); ele != myList.end(); ++ele) {
		std::cout << *ele << " ";
	}

	std::cout << "\n=== 基于for-each的输出：\n";
	for (int var : myList)
	{
		std::cout << var << " ";
	}

	for (int i = 0; i < myList.size(); ++i) {
		//        std::cout << ls[i] << std::endl;	// list不可以这么用,vector可以这么用
		//std::cout << myList.get_allocator << std::endl;
	}

	// 获取list链表的头节点
	int hNode = myList.front();
	std::allocator<int> ll = myList.get_allocator(); // 获取list的分配器 

	int* ary = myList.get_allocator().allocate(3); // 使用分配器创建数组

	int t = ary[0];

}

// list和数组，动态数组互转
void test23_StlConOfList::listToArray()
{
	std::cout << "list和数组互转\n";
	// list常用操作
	//std::list<std::string> strList;
	//std::list<int> myList;
	for (int i = 0; i < 10; i++)
	{
		auto ss = std::to_string(i);
		std::string s1 = "ss" + (i + 3); // 会有问题
		std::cout << s1 << " = ";
		std::string s2 = "ss" + std::to_string(i + 3); // 可以拼接成功
		std::cout << s2 << "--";
		
		std::string tmp = "ss" + (i+3) + std::to_string(i);
		//strList.push_back(tmp); // 把元素插入list的尾部
		std::cout << tmp << " - ";
	}
	// 报错
	// 0 pop_back called on empty list1 op_back called on empty list2 p_back called on empty list3 _back called on empty list4 
	// back called on empty list5 ack called on empty list6 ck called on empty list7 k called on empty list8  called on empty list9

	int ary[20] = {};

	//std::cout << "\n===\n";
	//for (std::string var : strList)
	//{
	//	std::cout << var << " ";
	//}
}